package com.lzy.crm.base.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * 登录失效处理
 * @author csp
 *
 */
public class LoginFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		HttpSession session = request.getSession();
		// 如果session不为空，则可以浏览其他页面
		String url = request.getServletPath();
		//System.out.println(url);
		//这里判断目录，后缀名，当然也可以写在web.xml中，用url-pattern进行拦截映射 跳过微信模块页面
		if ((request.getServletPath().indexOf("/group/")==-1)&&
				(request.getServletPath().indexOf("/gm/")==-1)&&
				(request.getServletPath().indexOf("/wechat/")==-1)
				&& (!request.getServletPath().equals("/admin/login.jsp"))
				&& (!request.getServletPath().equals("/validateCode/getValidateCode.html"))
				&& (!request.getServletPath().equals("/admin/login.html"))
				) {
			//System.out.println(request.getServletPath());
			if (session.getAttribute("userSession") == null) {
				//session.invalidate();
				/*response.setContentType("text/html;charset=utf-8");
				PrintWriter out = response.getWriter();
				out.println("<script language='javascript' type='text/javascript'>");
				out.println("alert('由于你长时间没有操作,导致Session失效!请你重新登录!');window.location.href='" + request.getContextPath() + "/admin/login.jsp");
				out.println("</script>");*/
				/*String path = request.getContextPath();
				String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
				response.sendRedirect(basePath+"login.jsp");*/
				if(isAjaxRequest(request)){
					response.addHeader("sessionstatus", "timeOut");
					chain.doFilter(request, response);
				}else{
					String str = "<script language='javascript'>alert('会话过期，请重新登录！');"
		                    + "window.top.location.href='"+request.getContextPath()+"/login.jsp';</script>";
		            response.setContentType("text/html;charset=UTF-8");
		            try {
		                PrintWriter writer = response.getWriter();
		                writer.write(str);
		                writer.flush();
		                writer.close();
		            } catch (Exception e) {
		                e.printStackTrace();
		            }
		            return;
				}
			} else {
				chain.doFilter(request, response);
			}
		} else {
			chain.doFilter(request, response);
		}
		

		
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

	private boolean isAjaxRequest(HttpServletRequest request){  
	    String header = request.getHeader("X-Requested-With");
	    return "XMLHttpRequest".equalsIgnoreCase(header) ? true:false;  
	}
	
}
